package cn.dapeng.interview;

import cn.dapeng.util.SleepingUtil;

import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ArrayBlockingQueue_T01 {

    public static void main(String[] args) throws Exception {
        ArrayBlockingQueue<UUID> queue = new ArrayBlockingQueue<>(10);

        int putThreadNum = 2;
        for (int i = 0; i < putThreadNum; i++) {
            new Thread(() -> {
                for (; ; ) {
                    try {
                        queue.put(UUID.randomUUID());
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }

        for (int i = 0; i < putThreadNum; i++) {
            new Thread(() -> {
                while (true) {
                    try {
                        System.out.println("take :: " + queue.take());
                        SleepingUtil.sleep(1, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
        }
    }
}
